﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ToolBox
{
    public partial class SearchForm : Form
    {
        public int SearchType;
        public string SearchTerm;

        public SearchForm()
        {
            InitializeComponent();
        }

        private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            if (e.CurrentProgress < e.MaximumProgress)
            {
                if (tsSB.Value >= tsSB.Maximum)
                    tsSB.Value = tsSB.Minimum;
                else
                    tsSB.PerformStep();
            }
            else
            {
                tsSB.Value = tsSB.Minimum;
            }
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            tsSBText.Text = "Loading search...";
        }

        

        private void btnReload_Click(object sender, EventArgs e)
        {
            myBrowser.Refresh();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            myBrowser.Stop();
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            myBrowser.GoHome();
        }

        private void modifyNavigateButtons()
        {
            if (myBrowser.CanGoBack)
                btnBack.Enabled = true;
            else
                btnBack.Enabled = false;

            if (myBrowser.CanGoForward)
                btnForward.Enabled = true;
            else
                btnForward.Enabled = false;            

        }

        private void myBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            this.tsSBText.Text = "Loaded.";
            this.modifyNavigateButtons();
            this.txtURL.Text = myBrowser.Url.ToString();
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            this.myBrowser.Navigate(txtURL.Text);
        }

        private void doNavigate(string URL)
        {
            if (URL != "")
            {
                this.myBrowser.Navigate(URL);
            }                      
        }

        private void doGoogleSearch(string Term)
        {
            String CompletedTerm;
            CompletedTerm = "http://www.google.com/search?q=" + Term;
            this.txtURL.Text = CompletedTerm;
            doNavigate(CompletedTerm);            
        }

        private void doLiveSearch(string Term)
        {
            string CompletedTerm;
            CompletedTerm = "http://search.msn.com/results.aspx?q=" + Term;
            this.txtURL.Text = CompletedTerm;
            doNavigate(CompletedTerm);
        }

        private void doGoogleMapsSearch(string Term)
        {
            String CompletedTerm;
            CompletedTerm = "http://maps.google.co.uk/maps?q=" + Term;
            this.txtURL.Text = CompletedTerm;
            doNavigate(CompletedTerm);
        }

        private void doWikipediaSearch(string Term)
        {
            String CompletedTerm;
            CompletedTerm = "http://en.wikipedia.org/wiki/Special:Search?search=" + Term + "&fulltext=Search";
            this.txtURL.Text = CompletedTerm;
            doNavigate(CompletedTerm);
        }

        private void GMailSearch(string Term)
        {
            String CompletedTerm;
            CompletedTerm = "http://mail.google.com/mail/?source=navclient-ff#search/" + Term;
            this.txtURL.Text = CompletedTerm;
            doNavigate(CompletedTerm);
        }
      

        private void doSearchTypeCheck(int Type, string Term)
        {
            if (Type == 1)
            {                
                doGoogleSearch(Term);
            }
            if (Type == 2)
            {
                doLiveSearch(Term);
            }
            if (Type == 3)
            {
                doGoogleMapsSearch(Term);
            }
            if (Type == 4)
            {
                doWikipediaSearch(Term);
            }
            if (Type == 5)
            {
                GMailSearch(Term);
            }
            
        }

        private void SearchForm_Load(object sender, EventArgs e)
        {
            tsSearchTerm.Text = SearchTerm;
            doSearchTypeCheck(SearchType, SearchTerm);
            if (this.SearchType == 999)
                this.myBrowser.GoHome();
        }

        private void txtURL_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                this.myBrowser.Navigate(txtURL.Text);
            }
        }

        private void tsbtnGo_Click(object sender, EventArgs e)
        {
            ToolBarSearch();
            
        }

        private void ToolBarSearch()
        {
            int gotIndex = tsSearchSelect.SelectedIndex;
            if (gotIndex == 0)
            {
                this.SearchTerm = tsSearchTerm.Text;
                this.SearchType = 1;
            }
            if (gotIndex == 1)
            {
                this.SearchTerm = tsSearchTerm.Text;
                this.SearchType = 2;
            }
            if (gotIndex == 2)
            {
                this.SearchTerm = tsSearchTerm.Text;
                this.SearchType = 3;
            }
            if (gotIndex == 3)
            {
                this.SearchTerm = tsSearchTerm.Text;
                this.SearchType = 4;
            }

            if (gotIndex != -1)
            {
                doSearchTypeCheck(this.SearchType, this.SearchTerm);
            }

        }

        private void formSearch()
        {
            this.SearchTerm = tsSearchTerm.Text;
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.myBrowser.GoBack();
        }

        private void btnForward_Click(object sender, EventArgs e)
        {
            this.myBrowser.GoForward();
        }

        private void tsSearchTerm_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                ToolBarSearch();
        }
         
    }
}
